import {_decorator, Component, Node, input, Input, EventTouch, Sprite, Vec3, math, v3, CCFloat} from 'cc';
import {VirtualInput} from "db://assets/Script/Inputs/VirtualInput";

const {ccclass, property} = _decorator;

@ccclass('UIJoyStick')
export class UIJoyStick extends Component {
    @property(Sprite)
    joyStickPanel: Sprite = null!
    @property(Sprite)
    joyStickButton: Sprite = null!
    @property(CCFloat)
    radius: number = 120 //移动范围半径

    //初始化位置
    initJoyStickPosition: Vec3 = v3()

    onLoad() {
        this.node.on(Input.EventType.TOUCH_START, this.onTouchStart, this)
        this.node.on(Input.EventType.TOUCH_CANCEL, this.onTouchEnd, this)
        this.node.on(Input.EventType.TOUCH_MOVE, this.onTouchMove, this)
        this.node.on(Input.EventType.TOUCH_END, this.onTouchEnd, this)
        //获取默认状态下joyStickPanel的世界坐标
        //使用clone方法作用是如果对initJoyStickPosition坐标进行修改，不影响到joyStickPanel的世界坐标
        this.initJoyStickPosition = this.joyStickPanel.node.worldPosition.clone()
        console.log('默认状态下JoyStickPanel世界坐标：',this.initJoyStickPosition)
    }

    start() {
    }

    update(deltaTime: number) {


    }

    onDestory() {
        this.node.off(Input.EventType.TOUCH_START, this.onTouchStart, this)
        this.node.off(Input.EventType.TOUCH_CANCEL, this.onTouchEnd, this)
        this.node.off(Input.EventType.TOUCH_MOVE, this.onTouchMove, this)
        this.node.off(Input.EventType.TOUCH_END, this.onTouchEnd, this)
    }

    onTouchStart(event: EventTouch) {
        let locationX = event.touch.getUILocationX();
        let locationY = event.touch.getUILocationY();
        //当前UI坐标位置转为世界坐标位置
        this.joyStickPanel.node.setWorldPosition(locationX, locationY, 0)
    }

    onTouchMove(event: EventTouch) {
        let locationX = event.touch.getUILocationX()
        let locationY = event.touch.getUILocationY()
        let worldPosition = new Vec3(locationX, locationY, 0)
        //初始化本地坐标
        let localPosition = v3()
        //世界坐标-->本地坐标
        this.joyStickPanel.node.inverseTransformPoint(localPosition, worldPosition);
        //初始化button坐标
        let joyStickButtonPosition = v3()
        //获取本地向量长度
        let localLength = localPosition.length();
        //向量长度设置为1
        localPosition.normalize()
        // 将localPosition 加到 joyStickButtonPosition 缩放系数限制在joyStickButton的半径范围内
        Vec3.scaleAndAdd(joyStickButtonPosition, v3(), localPosition, math.clamp(localLength, 0, this.radius));
        //设置joyStickButton的位置
        this.joyStickButton.node.setPosition(joyStickButtonPosition)

        //触摸点x位置相对与按钮的位置比例
        VirtualInput.horizontal = this.joyStickButton.node.position.x / this.radius;
        VirtualInput.vertical = this.joyStickButton.node.position.y / this.radius;
        console.log("x轴y轴按钮位置：", VirtualInput.horizontal, VirtualInput.vertical)
    }

    onTouchEnd(event: EventTouch) {
        this.joyStickButton.node.setPosition(v3())
        VirtualInput.horizontal = 0
        VirtualInput.vertical = 0

        //重置位置
        this.joyStickPanel.node.worldPosition = this.initJoyStickPosition
    }
}


